# -*- coding:utf-8 -*-
from flask import Blueprint, render_template, request, jsonify, session, make_response, url_for,redirect
from apps.ext import db
from apps.models import News, Kind, Comment, User

news = Blueprint('news', __name__)


@news.route('/', methods=['GET', 'POST'])
def details():
    # 查询用户登录状态
    user = session.get('user')
    if request.method == 'GET':
        new_id = request.values.get('new')
        kind_list = Kind.query.all()
        new = News.query.filter_by(hash_id=new_id).first()
        # 点击量+1
        new.click_num += 1
        db.session.commit()
        new_type = Kind.query.filter_by(type_id=new.type_id).first()
        hot_news = News.query.filter_by(type_id=new.type_id).limit(10).all()
        comments = Comment.query.filter_by(hash_id=new_id).all()
        if user:
            user = User.query.filter_by(user_id=user['user_id']).first()
            if new in user.have_store:
                fav = "取消收藏"
            else:
                fav = '收藏'
        else:
            fav = '收藏'
        if comments:
            items = []
            # 将用户信息和对应评论生成一个字典，放入一个空列表
            for comment in comments:
                comments_user = User.query.filter_by(user_id=comment.user_id).first()
                item = {
                    "comments_user": comments_user,
                    "comment": comment
                }
                items.append(item)
        else:
            items = []
        return render_template("news/new.html", comments=comments, new=new, kind_list=kind_list,
                               new_type=new_type, fav=fav,
                               new_id=new_id, hot_news=hot_news, user=user,items=items)
    else:
        if request.form.get("post_type") == "comment":
            if user:
                # 获取用户信息
                comments = request.form.get('comment')
                new_id = request.form.get('new_id')
                new = News.query.get(new_id)
                new.comment_num += 1
                user_comment = Comment(content=comments, user_id=user["user_id"], hash_id=new_id)
                db.session.add(user_comment)
                db.session.commit()
                return jsonify({"status": "success", "comments": comments})
            else:
                return jsonify({"status": "fail"})
        else:
            if user:
                if request.form.get('is_fav') == "收藏":
                    new_id = request.form.get('new_id')
                    new = News.query.filter_by(hash_id=new_id).first()
                    new.store_up_num += 1
                    user = User.query.filter_by(user_id=user['user_id']).first()
                    user.store_num += 1
                    user.have_store.append(new)
                    db.session.commit()
                    return jsonify({"status": "success", "msg": "取消收藏"})
                else:
                    new_id = request.form.get('new_id')
                    user = User.query.filter_by(user_id=user['user_id']).first()
                    user.store_num -= 1
                    new = News.query.get(new_id)
                    new.store_up_num -= 1
                    user.have_store.remove(new)
                    db.session.commit()
                    return jsonify({"status": "success", "msg": "收藏"})
            else:
                return jsonify({"status": "fail"})
